// controllers/msg_list_controller.dart
import 'package:chat/model/message_model.dart';
import 'package:get/get.dart';

class MessageListController extends GetxController {
  final RxList<Message> messages = <Message>[].obs;
  final RxString searchQuery = ''.obs;

  @override
  void onInit() {
    super.onInit();
    // 初始化数据
    _loadMessages();
  }

  void _loadMessages() {
    // 模拟数据
    final now = DateTime.now();
    messages.assignAll([
      Message(
        id: '1',
        senderId: '1',
        senderName: '官方消息',
        senderAvatar: 'assets/images/zh-CN-XiaochenNeural.png',
        content: 'is it gonna dip?',
        time: now.subtract(const Duration(minutes: 3)),
        isOfficial: true,
      ),
      Message(
        id: '2',
        senderId: '2',
        senderName: 'seth.eth',
        senderAvatar: 'assets/images/zh-CN-XiaohanNeural.png',
        content: 'is it gonna dip?',
        time: now.subtract(const Duration(minutes: 3)),
      ),
      Message(
        id: '3',
        senderId: '3',
        senderName: 'pacmanNFT',
        senderAvatar: 'assets/images/zh-CN-XiaomoNeural.png',
        content: "let's have a gcc",
        time: now.subtract(const Duration(hours: 2)),
      ),
    ]);
  }

  void refreshMessages() {
    _loadMessages();
  }

  List<Message> get filteredMessages {
    if (searchQuery.isEmpty) return messages;
    return messages
        .where(
          (message) =>
              message.senderName.toLowerCase().contains(searchQuery.toLowerCase()) ||
              message.content.toLowerCase().contains(searchQuery.toLowerCase()),
        )
        .toList();
  }
}
